﻿
$.ajaxSetup({
    type: "POST"
});

//------------------------------------------------------ShowMsg显示信息

function addMsg(obj, msg, css) {
    var thisObj = $(obj);
    thisObj.nextAll().remove();
    var p = $("<span>").text(msg).addClass(css);
    thisObj.after(p);
}

//提示插件
jQuery.fn.extend({
    //出错信息
    addShowMsg: function (msg) {
        addMsg(this, msg, "hint_show");
    },
    //提示信息
    addInfoMsg: function (msg) {
        addMsg(this, msg, "hint_info");
    },
    addSuccessMsg: function (msg) {
        addMsg(this, msg, "");
    }
});

//-------------------------------------------------End--ShowMsg显示信息

//------------------------------------------------------Validate验证插件，使用到ShowMsg插件

//设置一个自定义空对象
var nullObj = $();

function returnValidate(obj, option) {
    var thisObj = $(obj);
    //判断是否为空对象
    if (thisObj.length != 0) {
        var value = $.trim(thisObj.val());
        var title = thisObj.attr("title");
        //判断验证类型
        switch (option.type) {
            case "validateNull":
                if (value.length == 0) {
                    thisObj.addShowMsg(title + "不能为空！");
                    return nullObj;
                }
                break;
            case "validateLength":
                if (value.length < option.min || value.length > option.max) {
                    thisObj.addShowMsg(title + "字符数在" + option.min + "-" + option.max + "之间！");
                    return nullObj;
                }
                break;
            case "validateReg":
                if (value.search(option.reg) == -1) {
                    thisObj.addShowMsg(title + (option.msg.length == 0 ? "格式不正确！" : option.msg));
                    return nullObj;
                }
                break;
            case "validateEmail":
                if (value.search(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/) == -1) {
                    thisObj.addShowMsg("Email格式不正确！");
                    return nullObj;
                }
                break;
            case "validateMobile":
                if (value.search(/[1]+[3,5,8,4]+\d{9}$/) == -1) {
                    thisObj.addShowMsg("手机号码格式不正确！");
                    return nullObj;
                }
                break;
            case "validateInt":
                if (value.search(/\d+$/) == -1) {
                    thisObj.addShowMsg(title + "需要数字类型！");
                    return nullObj;
                }
                break;
            case "validateCompare":
                var comObj = $(option.comobj);
                if (value != comObj.val()) {
                    thisObj.addShowMsg(title + "与" + comObj.attr("title") + "不一致！");
                    return nullObj;
                }
                break;
            case "validateUserCompare":
                var comObj = $(option.comobj);
                var comTjObj = $(option.comTjObj);
                if (comTjObj.val() == comObj.val()) {
                    thisObj.addShowMsg(title + "与" + comObj.attr("title") + "不能一致！");
                    return nullObj;
                }
                break;
            case "validateCheck":
                if (thisObj.attr("checked") != true && thisObj.attr("checked") != "checked") {
                    thisObj.parent().addShowMsg(title + "需要选中！");
                    return nullObj;
                }
                thisObj = thisObj.parent();
                break;
            //注意暂时只能针对性的，不具有泛性 
            case "validateAjax":
                var ajaxFlag = false;
                $.ajax({
                    url: option.url,
                    async: option.async,
                    data: option.data,
                    dataType: "json",
                    success: function (result) {
                        if (result.d == "true") {
                            thisObj.addShowMsg(title + (option.msg.length == 0 ? "验证失败！" : option.msg));
                            ajaxFlag = true;
                        }
                    },
                    error: function () {
                        thisObj.addShowMsg(title + "验证出错！");
                        ajaxFlag = true;
                    }
                });
                if (ajaxFlag)
                    return nullObj;
                break;
        }
        thisObj.addSuccessMsg("");
    }
    return thisObj;
}

//验证插件-----以后扩展上对大部分控件的验证
jQuery.fn.extend({
    //是否为空
    validateNull: function () {
        return returnValidate(this, { type: "validateNull" });
    },
    //判断长度
    validateLength: function (min, max) {
        return returnValidate(this, { type: "validateLength", min: min, max: max });
    },
    //判断格式
    validateReg: function (reg, msg) {
        return returnValidate(this, { type: "validateReg", reg: reg, msg: msg });
    },
    //判断Email格式
    validateEmail: function () {
        return returnValidate(this, { type: "validateEmail" });
    },
    //判断手机格式
    validateMobile: function () {
        return returnValidate(this, { type: "validateMobile" });
    },
    //判断数字类型
    validateInt: function () {
        return returnValidate(this, { type: "validateInt" });
    },
    //判断是否与其他值一致
    validateCompare: function (id) {
        var comObj = $("#" + id);
        return returnValidate(this, { type: "validateCompare", comobj: comObj });
    },
    //判断用户名和推荐用户名是否一致
    validateUserCompare: function (id) {
        var comObj = $("#" + id);
        var comTjObj = $("#txtTjUser");
        return returnValidate(this, { type: "validateUserCompare", comobj: comObj, comTjObj: comTjObj });
    },
    //判断是否选择
    validateCheck: function () {
        return returnValidate(this, { type: "validateCheck" })
    },
    //进行ajax验证
    validateAjax: function (url, data, msg, async) {
        return returnValidate(this, { type: "validateAjax", url: url, data: data, msg: msg, async: async });
    }
});

//-------------------------------------------------End--Validate验证插件

//验证用户名
function checkUserName(ajax) {
    return username.validateNull().validateLength(2, 32).validateReg(/^([_0-9a-zA-Z\u4e00-\u9fa5]*)$/, "格式为：字母,数字，中文，下划线（_）").validateAjax("/Ashx/Login/RegisterHandler.ashx", { action: "checkUser", userName: encodeURIComponent(username.val()) }, "已经存在！", false).length == 0 ? false : true;
}

//验证推荐人用户名是否存在
function checkTjUserName(ajax) {
    var flagTj = true;
    if (tjUser.val().length > 0) {
        flagTj = tjUser.validateUserCompare("username").validateReg(/^([_0-9a-zA-Z\u4e00-\u9fa5]*)$/, "格式为：字母,数字，中文，下划线（_）").validateAjax("/Ashx/HdlLogin.ashx", { action: "checkTjUser", userName: encodeURIComponent(tjUser.val()) }, "不存在！", false).length == 0 ? false : true;
    }
    return flagTj;
}

//验证手机号码
function checkMobile(ajax) {
    return mobile.validateNull().validateMobile().validateAjax("/Ashx/Login/RegisterHandler.ashx", { action: "checkMobile", mobile: encodeURIComponent(mobile.val()) }, "已经存在！", false).length == 0 ? false : true;
}

//验证手机验证码
function checkMobileCode() {
    return mobileCode.validateNull();
}

//验证密码
function checkPassword() {
    return password.validateNull().validateLength(6, 32).validateReg(/^([_0-9a-zA-Z]*)$/, "").length == 0 ? false : true;
}

//验证确认密码
function checkRePassword() {
    return repassword.validateCompare("password").length == 0 ? false : true;
}

//验证接受协议
function checkAgree() {
    //return agree.validateCheck().length == 0 ? false : true;
}

//注册按钮验证
function checkRegister(hasCheck) {
    var canRegister = true;
    //canRegister = checkUserName() && canRegister;
    //canRegister = checkTjUserName() && canRegister;
    //canRegister = checkMobileCode() && canRegister;
    canRegister = checkPassword() && canRegister;
    canRegister = checkRePassword() && canRegister;
    //    if (hasCheck)
    //        canRegister = checkAgree() && canRegister;
    //alert(canRegister);
    return canRegister;
}

// 个人注册
function registerUserPer() {
    if (checkRegister(true)) {
        EditPassword(registerper, "RegisterPer", "/User/Index.aspx");
    }
    return false;
}

//找回密码-修改密码
function EditPassword(obj, registerType, defaultUrl) {
    var defaultUrl = "http://user.daojutong.com/Login.aspx";
    var url = "http://user.daojutong.com/Ashx/Login/ForgetPasswordHandler.ashx";

    //异步注册
    $.ajax({
        url: url,
        async: false,
        data: { action: "editPassword", newPassword: encodeURIComponent(password.val()), againPassword: encodeURIComponent(repassword.val()) },
        dataType: "json",
        beforeSend: function () {
            $("#showInfo").html("修改中...");
        },
        success: function (result) {
            if (result.d == "true") {
                $("#showInfo").html("恭喜您,密码修改成功!请妥善保管好您的密码!");
                window.location.href = "http://user.daojutong.com/Login.aspx";
            }
            else {
                $("#showInfo").html(result.msg);
            }
        },
        error: function (httpRequest, textStatus, errorThrown) {
            $("#showInfo").html("注册过程出错，请重新注册！");
        }
    });
}


//定义对象
var password;
var repassword;
var registerper;

//初始化
$(function () {

    //定义对象
    password = $("#password");
    repassword = $("#repassword");
    registerper = $("#btnForget");
    showInfo = $("#showInfo");
    password.blur(function () {
        checkPassword();
        //同时验证
        checkRePassword();
    }).focus(function () {
        $(this).addInfoMsg("");
    });

    repassword.blur(function () {
        checkRePassword();
    }).focus(function () {
        $(this).addInfoMsg("");
    });


    //绑定修改密码事件
    //    registerper.bind("click", function () {
    //        alert("000000");
    //        registerUserPer();
    //    });

});
    
     